package fr.lostnet.mapview;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class GoogleMap implements RemoteListener {
	
	private static final String GOOGLE_MAPS_URL = "https://maps.googleapis.com/maps/api";
	private static final String LOG_TAG = "GoogleMap";
	
	public void test() {
		Log.i(LOG_TAG, "Calling web service");
		new RemoteService(this).execute(GOOGLE_MAPS_URL + 
				"/geocode/json?address=6+rue+de+la+Clery,+Nemours,+FR&sensor=false");
	}
	
	private void parseJson(String data) {
	    try {
			JSONObject json = new JSONObject(data);
			JSONArray names = json.names();
			JSONArray values = json.toJSONArray(names);
			for (int i = 0; i < names.length(); i++) {
				Log.i("LOG_TAG", "'" + names.getString(i) + "': '" + values.getString(i) + "'");
			}
		} catch (JSONException e) {
			Log.e("LOG_TAG", "Error while parsing JSON");
		}
	}
	
/*
 * 06-15 15:26:00.235: INFO/GoogleMap(26069): Result of query: 
 * [{   "results" : [      
 * 		{         "address_components" : [            
 * 			{               "long_name" : "1600",               
 * 							"short_name" : "1600",               
 * 							"types" : [ "street_number" ]            
 * 			},            
 * 			{               "long_name" : "Amphitheatre Pkwy",               
 * 							"short_name" : "Amphitheatre Pkwy",               
 * 							"types" : [ "route" ]            
 * 			},            
 * 			{               "long_name" : "Mountain View",               
 * 							"short_name" : "Mountain View",               
 * 							"types" : [ "locality", "political" ]            
 * 			},            
 * 			{               "long_name" : "San Jose",               
 * 							"short_name" : "San Jose",              
 * 							"types" : [ "administrative_area_level_3", "political" ]            
 * 			},            
 * 			{               "long_name" : "Santa Clara",               
 * 							"short_name" : "Santa Clara",               
 * 							"types" : [ "administrative_area_level_2", "political" ]            
 * 			},            
 * 			{               "long_name" : "California",               
 * 							"short_name" : "CA",               
 * 							"types" : [ "administrative_area_level_1", "political" ]            
 * 			},            
 * 			{              "long_name" : "United States",               
 * 							"short_name" : "US",               
 * 							"types" : [ "country", "political" ]            
 * 			},            
 * 			{               "long_name" : "94043",               
 * 							"short_name" : "94043",               
 * 							"types" : [ "postal_code" ]            
 * 			}         
 * 		],         
 * 		"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",         
 * 		"geometry" : {            
 * 			"location" : {               
 * 				"lat" : 37.42130680,               
 * 				"lng" : -122.085290            
 * 			},            
 * 			"location_type" : "ROOFTOP",            
 * 			"viewport" : {               
 * 				"northeast" : {                  
 * 					"lat" : 37.42445442068016,                  
 * 					"lng" : -122.0821423793198               
 * 			},               
 * 			"southwest" : {                  
 * 					"lat" : 37.41815917931982,                  
 * 					"lng" : -122.0884376206802               
 * 			}            
 * 		}         
 * },         
 * "types" : [ "street_address" ]      }   ],   
 * "status" : "OK"}]
 */

	@Override
	public void onRequestComplete(String result) {
		// TODO Auto-generated method stub
		Log.i(LOG_TAG, "Web service call completed");
		parseJson(result);
	}

}
